const http = require('http')
const fs = require('fs')
const path = require('path')

const server = http.createServer()

function render(res, url) {
    res.writeHeader(200, { 'Content-Type': 'text/html;charset=utf-8' }) //设置响应头
    fs.readFile(path.join(__dirname, url), (err, data) => {
        if (err) {
            console.log(err)
        } else {
            //渲染页面
            res.write(data)
            res.end()
        }
    })
}
function router(req, res) {
    server.on('request', (req, res) => {
        let url = req.url
        if (url === '/home') {
            render(res, './static/home.html')
        }
        else if (url === '/login') {
            render(res, './static/login.html')
        }
        else {
            render(res, './static/404.html')
        }
    })
}

export default router